package com.example.drink_order_system.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.drink_order_system.R;
import com.example.drink_order_system.activity.JiFen_detail;
import com.example.drink_order_system.activity.MainActivity;
import com.example.drink_order_system.activity.Message;
import com.example.drink_order_system.activity.PhotoPreview;
import com.example.drink_order_system.activity.User_manage;
import com.example.drink_order_system.activity.discount_detail_page;
import com.example.drink_order_system.activity.get_problems;
import com.example.drink_order_system.entity.discount_quan;
import com.example.drink_order_system.entity.task;
import com.example.drink_order_system.tool.ImageUtil;

import DataBase.UserDbHelper;

public class MineFragment extends Fragment {
    private Button signIn;
    private RelativeLayout jiFen;
    private RelativeLayout discount;
    private Button message;
    private Button user_manage;
    private Button problem;
    private Button activity;
    private Button exit_login;
    private ImageView user_img;
    private TextView user_name;
    private TextView show_score;
    private TextView discount_num;
    private static boolean is_signIn = false;



    public MineFragment() {
        // Required empty public constructor
    }

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        is_signIn = false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fg_mine, container, false);
        user_img = view.findViewById(R.id.user_img);
        user_name = view.findViewById(R.id.user_name);
        signIn = view.findViewById(R.id.qd_btn);
        jiFen = view.findViewById(R.id.jifen_ly);
        discount = view.findViewById(R.id.discount_part);
        message = view.findViewById(R.id.message);
        user_manage = view.findViewById(R.id.user_manage);
        problem = view.findViewById(R.id.problems);
        activity = view.findViewById(R.id.activity);
        exit_login = view.findViewById(R.id.exit_login);
        show_score = view.findViewById(R.id.show_score);
        discount_num = view.findViewById(R.id.discount_num);

        discount_num.setText(discount_quan.getDiscountQuans().size()+"\n优惠券");
        user_img.setImageBitmap(ImageUtil.base64ToImage(MainActivity.now_user.getImg_source()));
        show_score.setText(MainActivity.now_user.getScore()+"\n古达积分");
        user_name.setText(MainActivity.now_user.getUsername());


        //头像预览
        user_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user_img.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(user_img.getDrawingCache());
                user_img.setDrawingCacheEnabled(false);
                String new_userImg = ImageUtil.imageToBase64(bitmap);
                Intent intent = new Intent(getActivity(), PhotoPreview.class);
                intent.putExtra("preview",new_userImg);
                startActivity(intent);
            }
        });


        signIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!is_signIn){
                    signIn.setText("已签到");
                    int score = MainActivity.now_user.getScore()+20;
                    //MainActivity.now_user.setScore(score);
                    UserDbHelper db = (UserDbHelper) UserDbHelper.getSqliteHelper(getContext());
                    //db.saveScore(MainActivity.now_user.getUsername(),score);
                    show_score.setText(score+"\n古达积分");
                    Toast.makeText(getContext(),"签到成功！古达积分+20！",Toast.LENGTH_SHORT).show();
                    signIn.setBackgroundResource(R.drawable.is_sign_in);
                    signIn.setEnabled(false);
                    is_signIn = true;
                    if (!task.getTasks().get(0).isIs_done()){
                        task.getTasks().get(0).setIs_done(true);
                        //刷新积分并保存
                        MainActivity.now_user.setScore(MainActivity.now_user.getScore()+task.getTasks().get(0).getScore());
                        //测试语句
                        //MainActivity.now_user.setScore(MainActivity.now_user.getScore()+20000);
                        db.saveScore(MainActivity.now_user.getUsername(),MainActivity.now_user.getScore());
                    }
                }
            }
        });

        jiFen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), JiFen_detail.class);
                startActivity(intent);
            }
        });

        discount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), discount_detail_page.class);
                startActivity(intent);
            }
        });

        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Message.class);
                startActivity(intent);
            }
        });

        user_manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), User_manage.class);
                startActivity(intent);
            }
        });

        problem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), get_problems.class);
                startActivity(intent);
            }
        });
        exit_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
                Toast.makeText(getContext(),"成功退出登录！",Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        discount_num.setText(discount_quan.getDiscountQuans().size()+"\n优惠券");
        show_score.setText(String.valueOf(MainActivity.now_user.getScore())+"\n古达积分");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        discount_num.setText(discount_quan.getDiscountQuans().size()+"\n优惠券");
        show_score.setText(String.valueOf(MainActivity.now_user.getScore())+"\n古达积分");
    }
}